<template>
  <div
      class="infinite-list"
      v-infinite-scroll="loadMerchantCommodity"
      infinite-scroll-distance="10"
      :infinite-scroll-disabled="disabled"
  >
    <el-row :gutter="20">
      <el-col
          v-for="(Commodity) in merchantCommodityList"
          :key="Commodity"
          :span="6"
      >
        <el-card :body-style="{ padding: '0px' }" shadow="hover" @click="gotoDetails(Commodity.uuid)">
          <el-image :src="this.staticResourcesUrl+Commodity.img" fit="cover" class="card-img">
            <template #error>
              <div class="image-slot">
                <el-icon><picture /></el-icon>
              </div>
            </template>
          </el-image>
          <div class="card-info">
            <span>品名：{{Commodity.name}}</span>
            <span>售价：￥ {{Commodity.price}}</span>
            <span>上架日期：{{Commodity.time}}</span>
          </div>
          <div class="delBtn">
            <el-button type="danger" size="small" circle @click.stop="deleteCommodity(Commodity.uuid)" ><el-icon><delete /></el-icon></el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { reactive, getCurrentInstance } from 'vue'
import request from "@/http/request";

const {useRouter} = require("vue-router");

export default {
  name: "ManageCommodity",
  data() {
    return {
      loading: false,
      disabled: false,
      merchantCommodityList: reactive([]),
      merchantCommodityListMaxLen: 0,
    }
  },
  methods: {
    loadMerchantCommodity() {
      if (this.loading === false) {
        this.loading = true;
        if (this.pages>0 && this.pageNum > this.pages) {
          this.$tipUtil.infoTip("已加载您的所有上架商品");
          this.disabled = true;
        } else {
          request({
            "url": "commodity/getMerchantCommodity",
            "method": "get",
            params: {
              pageNo: this.merchantCommodityList.length,
            }
          }).then((response) => {
            if (response.code === 200) {
              this.merchantCommodityListMaxLen = response.data.total;
              this.merchantCommodityList = response.data.data.map(function (item) {
                return item;
              });
            } else {
              this.$tipUtil.errTip(response.msg);
            }
          })
        }
      }
    },
    deleteCommodity(uuid) {
      if (uuid!==null && uuid!=="") {
        request({
          "url": "commodity/deleteCommodity",
          "method": "post",
          data: {
            uuid: uuid,
          }
        }).then((response) => {
          if (response.code === 200) {
            this.$tipUtil.successTip(response.msg);
            this.merchantCommodityList.map((item)=>{
              if (item.uuid === uuid) {
                this.merchantCommodityList.splice(this.merchantCommodityList.indexOf(item), 1);
              }
            })
          } else {
            this.$tipUtil.errTip(response.msg);
          }
        })
      }
    }
  },
  setup(){
    const { appContext : { config: { globalProperties } } } = getCurrentInstance();
    const staticResourcesUrl = globalProperties.$staticResourcesUrl;
    const router = useRouter();
    const gotoDetails = function (uuid) {
      router.push({
        name: "UpdateCommodity",
        params: {
          uuid: uuid,
        }
      })
    }
    return {
      staticResourcesUrl,
      gotoDetails,
    }
  },
}
</script>

<style scoped>
.infinite-list {
  height: 93vh;
  margin: 0;
  padding: 20px 10px 0;
  overflow: auto;
  box-sizing: border-box;
}
.el-col{
  margin-bottom: 10px;
}
.el-col:last-child{
  margin-bottom: 0;
}
.card-img {
  height: 180px;
  min-width: 100%;
}
.card-info {
  height: 70px;
  padding: 7px 0;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}
.card-info > span:nth-child(1){
  font-size: 16px;
  font-weight: 700;
  color: #53a8ff;
}
.card-info > span:nth-child(2){
  font-size: 14px;
  color: #ff6363;
}
.card-info > span:nth-child(3){
  font-size: 14px;
  color: #7a7a7a;
}
.delBtn {
  margin-bottom: 5px;
}
</style>